package com.deyinet.bdcms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.deyinet.bdcms.controller.dtoandapi.MenuChildren;
import com.deyinet.bdcms.controller.dtoandapi.MenusApi;
import com.deyinet.bdcms.controller.dtoandapi.RoleRightApi;
import com.deyinet.bdcms.entity.Syslog;
import com.deyinet.bdcms.entity.Sysmodule;
import com.deyinet.bdcms.entity.Sysrole;
import com.deyinet.bdcms.entity.Sysuser;
import com.deyinet.bdcms.service.ILogsService;
import com.deyinet.bdcms.service.ISysmoduleService;
import com.deyinet.bdcms.service.ISysrolerightService;
import com.deyinet.bdcms.service.ISysrolesService;
import com.deyinet.bdcms.tools.WebUtilify;
import com.deyinet.bdcms.tools.superUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/18 0018.
 */
@Controller
@RequestMapping(value = "/sysmanage/sysmodule")
public class SysModuleController {

    @Autowired
    private ISysmoduleService sysmoduleService;
    @Autowired
    private ISysrolesService sysrolesService;
    @Autowired
    private ISysrolerightService sysrolerightService;
    @Autowired
    private ILogsService logsService;

    @RequestMapping(value = "/getusermenudata")
    @ResponseBody
    public String getusermenudata(HttpServletRequest request) {
        Sysuser user = WebUtilify.LoginUserinfo(request);
        List<Sysmodule> sysmodules;
        if (user.getRoleId() != superUser.getRoleId()) {
            sysmodules = sysmoduleService.getusermenudata(user.getSysroleByRoleid());
        } else {
            sysmodules = sysmoduleService.getList();
        }
        Map<String, MenusApi> map = getUserMenu(sysmodules);
        MenusApi[] apis = new MenusApi[map.size()];
        for (String key : map.keySet()) {
            apis[map.get(key).getIndex()] = map.get(key);
        }
        String s = JSONArray.toJSONString(apis);
        return s;
    }

    private Map<String, MenusApi> getUserMenu(List<Sysmodule> sysmodules) {
        Map<String, MenusApi> menusApis = new HashMap<>();
        int index = 0;
        for (Sysmodule sysmodule : sysmodules) {
            if (sysmodule.getSysmoduleByParentid() == null) {
                String sysmoduleName = sysmodule.getName();
                if (menusApis.get(sysmoduleName) == null) {
                    MenusApi menusApi = new MenusApi();
                    menusApi.setIndex(index);
                    index++;
                    menusApi.setName(sysmodule.getName());
                    menusApis.put(sysmoduleName, menusApi);
                }
            }
        }
        for (Sysmodule sysmodule : sysmodules) {
            if (sysmodule.getSysmoduleByParentid() != null) {
                String pname = sysmodule.getSysmoduleByParentid().getName();
                String cname = sysmodule.getName();
                MenusApi api = menusApis.get(pname);
                List<MenuChildren> children = api.getChildren();
                MenuChildren menuChildren = new MenuChildren(sysmodule.getNavtabid(), cname, sysmodule.getLinkurl());
                children.add(menuChildren);
            }
        }
        return menusApis;
    }

    @RequestMapping(value = "/list")
    public void list(Model model, int id) {
        model.addAttribute("rid", id);
    }

    @RequestMapping(value = "/listdata")
    @ResponseBody
    public List<Sysmodule> listdata(Sysrole role) {
        List<Sysmodule> all = sysmoduleService.getList();
        List<Sysmodule> checked = sysmoduleService.getusermenudata(role);
        for (int i = 0; i < checked.size(); i++) {
            for (int j = 0; j < all.size(); j++) {
                if (checked.get(i) == all.get(j)) {
                    all.get(j).setIscheck(1);
                }
            }
        }
        return all;
    }

    @RequestMapping(value = "/setRoleRight")
    @ResponseBody
    public Map<String, String> setRoleRight(@RequestBody RoleRightApi roleRightApi, HttpServletRequest request) {
        Syslog log = new Syslog();
        System.out.println(roleRightApi.getArr().length);
        sysrolerightService.setRoleRight(roleRightApi.getArr(), roleRightApi.getRid());
        log.setTitle("为id为" + roleRightApi.getRid() + "的角色分配权限");
        Sysmodule sysmodule = new Sysmodule();
        sysmodule.setId(11);
        log.setSysmoduleByModuleid(sysmodule);
        log.setSysuserByUserid(WebUtilify.LoginUserinfo(request));
        log.setContent(JSON.toJSONString(roleRightApi));
        log.setCreatedate(new Timestamp(System.currentTimeMillis()));
        WebUtilify.WriteLog(request,log, logsService);
        Map<String, String> info = new HashMap<>();
        info.put("Message", "ok");
        info.put("content", "用户权限分配成功");
        return info;
    }
}
